package com.example.blogsystem.service;

import com.example.blogsystem.entity.ArticleInfo;
import com.example.blogsystem.mapper.ArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    public int getArtCountById(Integer uid) {
        return articleMapper.getArtCount(uid);
    }

    public List<ArticleInfo> getMyBlogList(Integer uid) {
        List<ArticleInfo> list = articleMapper.getMyBlogList(uid);
        for (ArticleInfo art : list) {
            if (art.getContent().length() > 256) {
                art.setContent(art.getContent().substring(0, 256) + "...");
            }
        }
        return list;
    }

    public int deleteBlog(Integer id, Integer uid) {
        return articleMapper.deleteBlog(id, uid);
    }

    public ArticleInfo getArtDetail(Integer id) {
        return articleMapper.getArtDetail(id);
    }

    public int updateRCount(Integer id) {
        return articleMapper.updateRCount(id);
    }

    public int addBlog(ArticleInfo articleInfo) {
        return articleMapper.addBlog(articleInfo);
    }

    public int updateBlog(ArticleInfo articleInfo) {
        return articleMapper.updateBlog(articleInfo);
    }

    public List<ArticleInfo> getListArtByPage(Integer psize, Integer offsize) {
        List<ArticleInfo> list = articleMapper.getListArtByPage(psize, offsize);
        for (ArticleInfo art : list) {
            if (art.getContent().length() > 256) {
                art.setContent(art.getContent().substring(0, 256) + "...");
            }
        }
        return list;
    }

    public int getACount() {
        return articleMapper.getACount();
    }
}
